(function () {
    var vm = new Vue({
        el: "#wrapper",
        data: {
            sRegisterTip: '',
            sLoginTip: '',
            UserInfor: { Mobile: '', Name: '', Gender: 2, UserId: '', Password: '', CertCode: '', LoginCode: '', ValidateCode: '', Rememberme: '',Agree:'' },
            tab: true,
            showLoginCode:false,
            expireDays: '',
            p_obj: { url: 'logo', text: '提示文字' },
            loginDisable: true,
            validateDisable: true,
            registerDiable: true
        },
        mounted: function () {
            var _self = this;
            showHtml();
            createCode();
            var tipBox = document.getElementById("tipBox");
            var MobileTextBox = document.getElementById("Mobile");
            var p_loginType = getQueryString("loginType");
            var p_PhoneNo = getQueryString("PhoneNo");
            tipBox.style.display = "none";
            if (p_loginType != null) {
                checkLoginType(p_loginType);
            }
            
            if (p_PhoneNo != null) {                               
                MobileTextBox.value = p_PhoneNo;
                _self.UserInfor.Mobile = p_PhoneNo;
                _self.PhoneValidate();
            }          
        },
        methods: {
            //tab切换
            tabSwitch: function (val) {
                var _self = this;
                _self.UserInfor.Password = "";
                var loginTab = document.getElementById("loginTab"),
					registerTab = document.getElementById("registerTab"),
					login = document.getElementById("login"),
					register = document.getElementById("register"),
					tipBox = document.getElementById("tipBox");
                if (val == true) {
                    loginTab.setAttribute("class", "navActive");
                    registerTab.removeAttribute("class", "navActive");
                    login.style.display = "block";
                    register.style.display = "none";
                    tipBox.style.display = "none";
                } else if (val == false) {
                    loginTab.removeAttribute("class", "navActive");
                    registerTab.setAttribute("class", "navActive");
                    login.style.display = "none";
                    registerSuccess.style.display = "none";
                    register.style.display = "block";
                    tipBox.style.display = "none";
                }
            },
            //密码是否显示明文
            pswExpress: function (psw, pswPic) {
                var pswExpress = document.getElementById(pswPic),
					lPsw = document.getElementById(psw);
                console.log(lPsw)
                if (pswExpress.className == "noExpress") {
                    lPsw.setAttribute("type", "text");
                    pswExpress.style.background = "url(/styles/image/icon/psw_icon2.png) no-repeat";
                    pswExpress.setAttribute("class", "yesExpress");
                } else if (pswExpress.className == "yesExpress") {
                    lPsw.setAttribute("type", "password");
                    pswExpress.style.background = "url(/styles/image/icon/psw_icon1.png) no-repeat";
                    pswExpress.setAttribute("class", "noExpress");
                }
            },
            //注册成功填写信息
            registerValidation:function(p_agree){
                var _self = this,
                    registerSuccess = document.getElementById("registerSuccess"),
					register = document.getElementById("register"),
					login = document.getElementById("login"),
					successTipBox = document.getElementById("successTipBox"),
			        tipBox = document.getElementById("tipBox"),
                    agree = document.getElementById("agree");
                    currentValCode = "",
                    valRet = true;

                tipBox.style.display = "none";
                _self.registerDiable = true;

                if (isEmpty(_self.UserInfor.ValidateCode)) {
                    return false;
                }
                else if (isEmpty(_self.UserInfor.Password)) {
                    return false;
                }
                else if (agree.checked == false) {
                    return false;
                }
                //else if (_self.UserInfor.Agree == true){
                //    if (navigator.userAgent.indexOf("Firefox") > -1) {
                //        return false;
                //    }
                //}
                //else if (_self.UserInfor.Agree == false) {
                //    if (navigator.userAgent.indexOf("Firefox") === -1) {
                //        return false;
                //    }
                //}
               
                _self.registerDiable = false;
            },
            registerIn: function () {
                var _self = this,
                    tipBox = document.getElementById("tipBox");
                if (!(/^1[34578]\d{9}$/.test(_self.UserInfor.Mobile))) {
                    tipBox.style.display = "block";
                    _self.sLoginTip = "请输入正确格式的手机号码";
                    return false;
                }
                else if (!(/^[a-zA-Z\d]{8,20}$/.test(_self.UserInfor.Password))) {
                    tipBox.style.display = "block";
                    _self.sLoginTip = "请设置8~20位的字母或数字为密码";
                    return false;
                }

                _self.$http.post("/Login/RegisterNewUser", _self.UserInfor).then(function (res) {
                    if (res.body.Code == 1) {
                        tipBox.style.display = "none";
                        successTipBox.style.display = "block";
                        _self.sRegisterTip = _self.UserInfor.Mobile;
                        registerSuccess.style.display = "block";
                        register.style.display = "none";
                        login.style.display = "none";
                    }
                    else {
                        tipBox.style.display = "block";
                        _self.sLoginTip = res.body.Msg;
                    }
                });
            },
            //注册成功后补充信息
            submitExtra: function () {
                var _self = this;
                tipBox.style.display = "none";
                if (_self.UserInfor.Name != "") {
                    if (!(/[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*/.test(_self.UserInfor.Name))) {
                        tipBox.style.display = "block";
                        _self.sLoginTip = "请输入正确的姓名";
                        return false;
                    }
                }
                if(_self.UserInfor.CertCode != ""){
                    if (!(/(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$)/.test(_self.UserInfor.CertCode))) {
                        tipBox.style.display = "block";
                        _self.sLoginTip = "请输入正确的证件号码";
                        return false;
                    }
                }
                _self.$http.post("/Login/submitExtra", _self.UserInfor).then(function (res) {
                    _self.p_obj.url = "/styles/image/icon/ok.png";
                    _self.p_obj.text = "提交成功";
                    dialogTip(_self.p_obj);
                    setTimeout(function () {
                        if (window.history.length > 1)
                            window.history.back();
                        else
                            window.location.href = "..";
                    }, 1000);
                });
            },
            loginValidation:function(){
                var _self = this,
                    regUserId = "",
                    tipBox = document.getElementById("tipBox"),                   
                    loginCodeBox = document.getElementById("loginCodeBox");

                tipBox.style.display = "none";
                _self.loginDisable = true;
                if (isEmpty(_self.UserInfor.UserId)) {
                    return false;
                }
                else if (isEmpty(_self.UserInfor.Password)) {
                    return false;
                }           
                else if (_self.showLoginCode) {
                    if (isEmpty(_self.UserInfor.LoginCode)) {
                        return false;
                    }
                }
                _self.loginDisable = false;
            },
            //填写登录信息及验证
            userLogin: function () {
                var _self = this,
                    tipBox = document.getElementById("tipBox");
                if (!(/^1[34578]\d{9}$/.test(_self.UserInfor.UserId))) {
                    tipBox.style.display = "block";
                    _self.sLoginTip = "请输入正确的手机号！";
                    return false;
                }
                else if (!(/^[a-zA-Z\d]{8,20}$/.test(_self.UserInfor.Password))) {
                   tipBox.style.display = "block";
                   _self.sLoginTip = "密码长度必须为8~20位的字母或数字";
                   return false;
                }
                else if (_self.showLoginCode) {
                    if (_self.UserInfor.LoginCode.toUpperCase() != code.toUpperCase()) {
                        tipBox.style.display = "block";
                        _self.sLoginTip = "验证码输入有误！";
                        createCode();
                        return false;
                    }
                }
                _self.loginDisable = true;
                _self.$http.post("/Login/UserLogin", _self.UserInfor).then(function (res) {
                    if (res.body.Code == -1) {
                        tipBox.style.display = "block";
                        _self.sLoginTip = res.body.Msg;
                        _self.loginDisable = false;
                    }
                    else if (res.body.Code == 5) {
                        tipBox.style.display = "block";
                        loginCodeBox.style.display = "block";
                        _self.sLoginTip = res.body.Msg;
                        _self.showLoginCode = true;
                        _self.loginDisable = false;
                    }
                    else if (res.body.Code == 1) {
                        tipBox.style.display = "block";
                        _self.sLoginTip = res.body.Msg;
                        window.location.href = encodeURI("/Login/Index?loginType=reg&PhoneNo=" + _self.UserInfor.UserId);
                    }
                    else {                       
                        //登录成功后
                        _self.p_obj.url = "/styles/image/icon/ok.png";
                        _self.p_obj.text = "登录成功";
                        dialogTip(_self.p_obj);
                        setTimeout(function () {
                            if (window.history.length > 1) {
                                    window.history.back();
                            }
                            else
                                window.location.href = "..";
                        }, 1000);
                    }
                });
            },
            //返回上一个页面或首页
            skipBack: function () {
                if (window.history.length > 1)
                    window.history.back();
                else
                    window.location.href = "..";
            },
            PhoneValidate: function () {
                var _self = this;
                _self.validateDisable = true;
                if (isEmpty(_self.UserInfor.Mobile)) {
                    return false;
                }
                _self.validateDisable = false;
            },
            //获取验证码
            getValidateCode: function () {
                var _self = this;
                tipBox.style.display = "none";                
                
                if (!(/^1[34578]\d{9}$/.test(_self.UserInfor.Mobile))) {
                    tipBox.style.display = "block";
                    _self.sLoginTip = "请输入正确格式的手机号码";
                    return false;
                }
                
                tipBox.style.display = "none";
                _self.$http.post("/Login/GetValidateCode", _self.UserInfor).then(function (res) {
                    if(res.body.Code === -1)
                        _self.p_obj.url = "/styles/image/icon/no.png";
                    else if (res.body.Code === 2) {
                        tipBox.style.display = "block";
                        _self.sLoginTip = res.body.Msg;
                    }
                    else
                        _self.p_obj.url = "/styles/image/icon/ok.png";
                    _self.p_obj.text = res.body.Msg;
                    dialogTip(_self.p_obj);
                    if (res.body.Code == "1")
                        sendCode(document.getElementById("getValidateCode"));
                });
                
            }
        }
    })
})(Vue)